package br.urca.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

import br.urca.model.protocolo.ProtocoloSolicitacao;
import br.urca.util.Utils;

public class ProtocoloSolicitacaoHTTP {
	
	public static ProtocoloSolicitacao consultarRequerimentoColacao(String key, String periodo, String matricula) {
		try {
			URL url = new URL("http://"+Utils.SERVIDOR+"/protocolo/consulta/solicitacao/key="+key+",periodo="+periodo+",matricula="+matricula);
			InputStream resposta = url.openStream();
			return parseJSON(resposta);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ProtocoloSolicitacao solicitarColacao(String key, String periodo, String matricula) {
		try {
			URL url = new URL("http://"+Utils.SERVIDOR+"/protocolo/solicitacao/colacaograu/key="+key+",periodo="+periodo+",matricula="+matricula);
			InputStream resposta = url.openStream();
			return parseJSON(resposta);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static ProtocoloSolicitacao parseJSON(InputStream stream) {
		
		try {
			JSONObject jsonObject = new JSONObject(streamToString(stream));
			if (!jsonObject.isNull("resposta")) {
				ProtocoloSolicitacao ps = new ProtocoloSolicitacao();
				
				JSONObject jSolicitacao = jsonObject.getJSONObject("resposta");
				
				if (jSolicitacao.length() <= 0) {
					return null;
				}
				
				if (!jSolicitacao.isNull("numero"))
					ps.setNumero(jSolicitacao.getString("numero"));
				if (!jSolicitacao.isNull("periodo"))
					ps.setPeriodo(jSolicitacao.getString("periodo"));
				if (!jSolicitacao.isNull("matricula"))
					ps.setMatricula(jSolicitacao.getString("matricula"));
				if (!jSolicitacao.isNull("taxaCobrada"))
					ps.setTaxaCobrada(jSolicitacao.getString("taxaCobrada"));
				if (!jSolicitacao.isNull("valor"))
					ps.setValor(jSolicitacao.getDouble("valor"));
				if (!jSolicitacao.isNull("requerimento"))
					ps.setRequerimento(jSolicitacao.getString("requerimento"));
				if (!jSolicitacao.isNull("situacao"))
					ps.setSituacao(jSolicitacao.getString("situacao"));
				if (!jSolicitacao.isNull("quantidade"))
					ps.setQuantidade(jSolicitacao.getInt("quantidade"));
				if (!jSolicitacao.isNull("data")) {
					String data = jSolicitacao.getString("data").substring(0, 10);
					String ano, mes, dia = null;
					ano = data.substring(0,4);
					mes = data.substring(5,7);
					dia = data.substring(8,10);
					ps.setData(dia.concat("/").concat(mes).concat("/").concat(ano));
				}	
				
				return ps;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	private static String streamToString(InputStream inputStream)
			throws IOException {

		Charset charset = Charset.forName("ISO-8859-1");
		InputStreamReader stream = new InputStreamReader(inputStream, charset);
		BufferedReader reader = new BufferedReader(stream);
		StringBuffer responseBuffer = new StringBuffer();

		String read = "";
		while ((read = reader.readLine()) != null) {
			responseBuffer.append(read);
		}
		return responseBuffer.toString();
	}

}
